package club.emergency.wechat.department_staff.operate.consumer.controller;

import club.emergency.wechat.common.response.ResponseEntity;
import club.emergency.wechat.common.response.StatusCode;
import club.emergency.wechat.common.utils.JsonConvert;
import club.emergency.wechat.department_staff.operate.consumer.service.DepartmentStaffOperateConsumer;
import club.emergency.wechat.model.staff.DepartmentStaff;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.Objects;

/**
 * Create with IntelliJ IDEA
 * Project Name: wechat
 * Package Name: club.emergency.wechat.staff.operate.consumer.controller
 * Date: 2019/4/1
 *
 * @author: 快乐的小菌菌
 * Description:
 */
@Api("企业人员操作接口")
@RestController
@RequestMapping("/department-staff-operate")
public class DepartmentStaffOperateController {

    private DepartmentStaffOperateConsumer departmentStaffOperateConsumer;

    public DepartmentStaffOperateController(DepartmentStaffOperateConsumer departmentStaffOperateConsumer) {
        this.departmentStaffOperateConsumer = departmentStaffOperateConsumer;
    }

    @ApiOperation("新增企业人员")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "departmentId", value = "所属部门id", dataType = "long", paramType = "query"),
            @ApiImplicitParam(name = "departmentCode", value = "企业匹配字段", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "userName", value = "姓名", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "phone", value = "phone", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "staffNum", value = "工号", dataType = "string", paramType = "query")
    })
    @PostMapping("/saveDepartmentStaff")
    public ResponseEntity saveDepartmentStaff(
            @RequestParam(value = "departmentId", required = false) Long departmentId,
            @RequestParam(value = "departmentCode", required = false) String departmentCode,
            @RequestParam(value = "userName", required = false) String userName,
            @RequestParam(value = "phone", required = false) String phone,
            @RequestParam(value = "staffNum", required = false) String staffNum
    ) {
        DepartmentStaff departmentStaff = new DepartmentStaff();
        departmentStaff.setDepartmentCode(departmentCode);
        departmentStaff.setDepartmentId(departmentId);
        departmentStaff.setUserName(userName);
        departmentStaff.setPhone(phone);
        departmentStaff.setStaffNum(staffNum);
        return new JsonConvert<ResponseEntity>().jsonDecode(departmentStaffOperateConsumer.saveDepartmentStaff(departmentStaff), ResponseEntity.class);
    }

    @ApiOperation("修改企业人员")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "departmentId", value = "所属部门id", dataType = "long", paramType = "query"),
            @ApiImplicitParam(name = "departmentCode", value = "企业匹配字段", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "userName", value = "姓名", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "phone", value = "phone", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "staffNum", value = "工号", dataType = "string", paramType = "query")
    })
    @PostMapping("/modifyDepartmentStaff")
    public ResponseEntity modifyDepartmentStaff(
            @RequestParam(value = "id", required = false) Long id,
            @RequestParam(value = "departmentId", required = false) Long departmentId,
            @RequestParam(value = "departmentCode", required = false) String departmentCode,
            @RequestParam(value = "userName", required = false) String userName,
            @RequestParam(value = "phone", required = false) String phone,
            @RequestParam(value = "staffNum", required = false) String staffNum
    ) {
        DepartmentStaff departmentStaff = new DepartmentStaff();
        departmentStaff.setId(id);
        departmentStaff.setDepartmentCode(departmentCode);
        departmentStaff.setDepartmentId(departmentId);
        departmentStaff.setUserName(userName);
        departmentStaff.setPhone(phone);
        departmentStaff.setStaffNum(staffNum);
        departmentStaffOperateConsumer.saveDepartmentStaff(departmentStaff);
        return new JsonConvert<ResponseEntity>().jsonDecode(departmentStaffOperateConsumer.modifyDepartmentStaff(departmentStaff), ResponseEntity.class);
    }

    @ApiOperation("从Excel中导入企业人员")
    @PostMapping("/saveDepartmentStaffFromExcel")
    public ResponseEntity saveDepartmentStaffFromExcel(
            MultipartFile file,
            @RequestParam("departmentId") Long departmentId,
            @RequestParam("departmentCode") String departmentCode
    ) {
        if (Objects.isNull(file)) {
            return new ResponseEntity(StatusCode.UPLOAD_FILE_ERROR, "文件不能为空！");
        }
        String originalFilename = file.getOriginalFilename();
        if (Objects.isNull(originalFilename)) {
            return new ResponseEntity(StatusCode.DB_SAVE_ERROR, "获取文件名失败，请检查传入的文件方式是否正确！");
        }
        String suffix = originalFilename.substring(originalFilename.lastIndexOf(".") + 1);
        InputStream inputStream;
        byte[] bytes = null;
        try {
            inputStream = file.getInputStream();
            bytes = new byte[inputStream.available()];
            inputStream.read(bytes);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new JsonConvert<ResponseEntity>().jsonDecode(departmentStaffOperateConsumer.saveDepartmentStaffFromExcel(suffix, bytes, departmentId, departmentCode), ResponseEntity.class);
    }
}
